package com.mj.app.health.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.mj.app.health.R;
import com.mj.app.health.defination.IntentKeys;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ========================================
 * *
 * title: BaseActivity
 * description:
 * *
 * Created by David Hogan on 2018/3/2.
 * ========================================
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected Unbinder unbinder;
    private View rootView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rootView = LayoutInflater.from(this).inflate(setLayoutId(), null);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(rootView);

        unbinder = ButterKnife.bind(this);

        init();
    }

    public void redirectTo(Class<? extends Activity> activity) {
        redirectTo(activity, false);
    }

    public void redirectTo(Class<? extends Activity> activity, boolean isFinish) {

        redirectTo(activity, isFinish, null);
    }

    public void redirectTo(Class<? extends Activity> activity, boolean isFinish, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, activity);
        if (bundle != null) {
            intent.putExtra(IntentKeys.BUNDLE_KEY, bundle);
        }

        startActivity(intent);
        setAnimationIn();

        if (isFinish) {
            this.finish();
        }
    }

    public void setAnimationIn() {
        overridePendingTransition(R.anim.left_slide_in, R.anim.left_slide_out);
    }

    public void setAnimationOut() {
        overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
    }

    protected abstract int setLayoutId();
    protected abstract void init();

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setAnimationOut();
    }

    protected void back(){
        finish();
        setAnimationOut();
    }
}
